package hotciv.tools;

import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import minidraw.framework.DrawingEditor;
import minidraw.framework.Tool;
import minidraw.standard.NullTool;
import hotciv.framework.*;
import hotciv.view.GfxConstants;

public class MoveTool extends NullTool
{
	private Game game;
	private Position position;
	private Position previousPosition = null;
	
	
	public MoveTool(Game game, DrawingEditor editor)
	{
		this.game = game;
	}

	public void mouseUp(MouseEvent e, int x, int y)
	{
		int tileX = (x - GfxConstants.MAP_OFFSET_X)/GfxConstants.TILESIZE;
		int tileY = (y - GfxConstants.MAP_OFFSET_Y)/GfxConstants.TILESIZE;
		
		//Skips if trying to move outside the world
		if(tileX < GameConstants.WORLDSIZE && tileY < GameConstants.WORLDSIZE)
		{
			position = new Position(tileY, tileX);
			
			if(previousPosition != null)
			{
				if(game.getUnitAt(previousPosition) != null)
				{
					game.moveUnit(previousPosition, position);
				}
			}
			previousPosition = position;
		}
	}
}
